package cn.com.kh.batch.callback;

import cn.com.kh.FDDUtils;
import cn.com.kh.batch.BatchDeal_Utils;
import cn.com.kh.batch.BatchData_Utils;
import cn.com.kh.result.FddResult;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @Title: authCallback
 * @Description: 签署回调
 */
public class signCallback {
    private static final Log log = LogFactory.getLog(signCallback.class);

    private static int i_Num = 0;

    // 乙方签署回调
    public static JSONObject doBSignCallback(JdbcTemplate jt, String rec_Guid, String agreementDuid, String customerType, String callPram) {
        JSONObject res_Json = JSONObject.fromObject(new FddResult(FddResult.SUCCESS, "乙方签署回调处理完成", ""));
        try {

            //接收客户端调用参数
            JSONObject bizContentJson = FDDUtils.callbackParse(callPram).getJSONObject("bizContent");
            log.debug("♾法大大回调 乙方签署回调 处理开始 rec_Guid:" + rec_Guid + " agreementDuid:" + agreementDuid + " bizContentJson:" + bizContentJson);

            String docNo = FDDUtils.GetSFJV(bizContentJson, "docNo");   //获取合同编号
            String transactionNo = FDDUtils.GetSFJV(bizContentJson, "transactionNo");   //获取接入平台交易号
            String resultCode = FDDUtils.GetSFJV(bizContentJson, "resultCode"); //获取状态码
            String resultMsg = FDDUtils.GetSFJV(bizContentJson, "resultMsg");

            // 如果拒签获取拒签原因
            String rejectReason = FDDUtils.GetSFJV(bizContentJson, "rejectionReason");

            if (resultCode.equals("1") || resultCode.equals("10040019")) {
                i_Num = jt.update("update fdd_agreement_list set ht_state='已签署',ht_statetime=now(),transactionNo=?,resultCode=?,resultMsg=?,resultTime=now(),rejectionReason=? where agreementduid=?", transactionNo, resultCode, resultMsg, rejectReason, agreementDuid);
                log.debug("♾法大大回调 乙方签署回调 -乙方-" + customerType + "回调成功,结果更新到签署回调表-更新DB数据:" + i_Num);

                // 继续检查进行下一步推送签署(只做触发,不管结果)
                BatchDeal_Utils.pushSign(jt, rec_Guid);

            } else if (resultCode.equals("2")) { //已拒签
                i_Num = jt.update("update fdd_agreement_list set ht_state = '已拒签',ht_statetime=now(),transactionNo=?,resultCode=?,resultMsg=?,resultTime=now(),rejectionReason=? where agreementduid=?", transactionNo, resultCode, resultMsg, rejectReason, agreementDuid);
                log.debug("♾法大大回调 乙方签署回调 -乙方-" + customerType + "回调拒签,结果更新到签署回调表-更新DB数据:" + i_Num);

                // 记录主表批量处理进度-失败
                BatchData_Utils.updateStep_APICode(jt, "-6", resultCode, "已拒签-" + customerType + "(" + rejectReason + "-任务结束)", rec_Guid);

            } else {
                i_Num = jt.update("update fdd_agreement_list set ht_state = '拒签异常',ht_statetime=now(),transactionNo=?,resultCode=?,resultMsg=?,resultTime=now(),rejectionReason=? where agreementduid=?", transactionNo, resultCode, resultMsg, rejectReason, agreementDuid);
                log.debug("♾法大大回调 乙方签署回调 -乙方-" + customerType + "拒签异常,结果更新到签署回调表-更新DB数据:" + i_Num);

                // 记录主表批量处理进度-失败
                BatchData_Utils.updateStep_APICode(jt, "-6", resultCode, "拒签异常-" + customerType + "(" + rejectReason + "-任务结束)", rec_Guid);

            }

            // 检查是否全部完成签署且回调完成(只做触发,不管结果)
            BatchDeal_Utils.doTaskAllEndCheck(jt, rec_Guid);

        } catch (Exception e) {
            // 合同乙方签署回调处理异常
            BatchData_Utils.updateStep_APICode(jt, "-100", "-1", "合同乙方-" + customerType + "签署回调处理异常(" + e.getMessage() + ")", rec_Guid);

            res_Json = JSONObject.fromObject(new FddResult(FddResult.WARN, "乙方-" + customerType + "签署回调处理异常(" + e.getMessage() + ")", ""));

            log.error("♾法大大回调 乙方签署回调 乙方-" + customerType + "签署回调处理异常(" + e.getMessage() + ")", e);
        }
        return res_Json;
    }

    // 甲方签署回调
    public static JSONObject doASignCallback(JdbcTemplate jt, String rec_Guid, String agreementDuid, String customerType, String callPram) {
        JSONObject res_Json = JSONObject.fromObject(new FddResult(FddResult.SUCCESS, "甲方签署回调处理完成", ""));
        try {

            //接收客户端调用参数
            JSONObject bizContentJson = FDDUtils.callbackParse(callPram).getJSONObject("bizContent");
            log.debug("✅法大大回调 甲方签署回调 处理开始 rec_Guid:" + rec_Guid + " agreementDuid:" + agreementDuid + " bizContentJson:" + bizContentJson);

            String docNo = FDDUtils.GetSFJV(bizContentJson, "docNo"); // 获取合同编号
            String transactionNo = FDDUtils.GetSFJV(bizContentJson, "transactionNo"); // 获取接入平台交易号
            String resultCode = FDDUtils.GetSFJV(bizContentJson, "resultCode"); // 获取状态码
            String resultMsg = FDDUtils.GetSFJV(bizContentJson, "resultMsg"); // 获取状态描述

            // 如果拒签获取拒签原因
            String rejectReason = FDDUtils.GetSFJV(bizContentJson, "rejectionReason");

            if (resultCode.equals("1") || resultCode.equals("10040019")) {
                i_Num = jt.update("update fdd_agreement_list set ht_state='已签署',ht_statetime=now(),transactionNo=?,resultCode=?,resultMsg=?,resultTime=now(),rejectionReason=? where agreementduid=?", transactionNo, resultCode, resultMsg, rejectReason, agreementDuid);
                log.debug("✅法大大回调 甲方签署回调 -甲方-" + customerType + "回调成功,结果更新到签署回调表-更新DB数据:" + i_Num);

                // 继续检查进行下一步推送签署
                BatchDeal_Utils.pushSign(jt, rec_Guid);

                // 返回法大大回调结果
                res_Json = JSONObject.fromObject(new FddResult(FddResult.SUCCESS, "操作成功", res_Json));

            } else if (resultCode.equals("2")) { //已拒签
                i_Num = jt.update("update fdd_agreement_list set ht_state = '已拒签',ht_statetime=now(),transactionNo=?,resultCode=?,resultMsg=?,resultTime=now(),rejectionReason=? where agreementduid=?", transactionNo, resultCode, resultMsg, rejectReason, agreementDuid);
                log.debug("✅法大大回调 甲方签署回调 -甲方-" + customerType + "回调拒签,结果更新到签署回调表-更新DB数据:" + i_Num);

                // 记录主表批量处理进度-失败
                BatchData_Utils.updateStep_APICode(jt, "-7", resultCode, "已拒签-" + customerType + "(" + rejectReason + "-任务结束)", rec_Guid);

            } else {
                i_Num = jt.update("update fdd_agreement_list set ht_state = '拒签异常',ht_statetime=now(),transactionNo=?,resultCode=?,resultMsg=?,resultTime=now(),rejectionReason=? where agreementduid=?", transactionNo, resultCode, resultMsg, rejectReason, agreementDuid);
                log.debug("✅法大大回调 甲方签署回调 -甲方拒签异常,结果更新到签署回调表-更新DB数据:" + i_Num);

                // 记录主表批量处理进度-失败
                BatchData_Utils.updateStep_APICode(jt, "-7", resultCode, "拒签异常-" + customerType + "(" + rejectReason + "-任务结束)", rec_Guid);

            }

            // 检查是否全部完成签署且回调完成
            BatchDeal_Utils.doTaskAllEndCheck(jt, rec_Guid);

        } catch (Exception e) {
            // 合同甲方签署回调处理异常
            BatchData_Utils.updateStep_APICode(jt, "-100", "-1", "甲方-" + customerType + "签署回调处理异常(" + e.getMessage() + ")", rec_Guid);

            res_Json = JSONObject.fromObject(new FddResult(FddResult.WARN, "甲方-" + customerType + "签署回调处理异常(" + e.getMessage() + ")", ""));

            log.error("✅法大大回调 甲方签署回调 甲方-" + customerType + "签署回调处理异常(" + e.getMessage() + ")", e);
        }
        return res_Json;
    }

}
